package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.HomeworkComment;

public class HomeworkCommentRowMapper implements RowMapper<HomeworkComment> {

    @SuppressWarnings("unused")
    private int type;

    public HomeworkCommentRowMapper() {
    }

    public HomeworkCommentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public HomeworkComment mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseClass(new HomeworkComment(), rs);
    }

    public static HomeworkComment parseClass(HomeworkComment cl, ResultSet rs)
        throws SQLException {
        return parseClass(cl, rs, null);
    }

    public static HomeworkComment parseClass(HomeworkComment cl, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            cl.setChid(rs.getLong(su + "chid"));
        } catch (SQLException e) {
            cl.setChid(null);
        }
        try {
            cl.setHid(rs.getLong(su + "hid"));
        } catch (SQLException e) {
            cl.setHid(null);
        }
        try {
            cl.setUid(rs.getLong(su + "uid"));
        } catch (SQLException e) {
            cl.setUid(null);
        }
        try {
            cl.setContent(rs.getString(su + "content"));
        } catch (SQLException e) {
            cl.setContent(null);
        }
        try {
            cl.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            cl.setImg(null);
        }
        try {
            cl.setCname(rs.getString(su + "cname"));
        } catch (SQLException e) {
            cl.setCname(null);
        }
        try {
            cl.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            cl.setCdate(null);
        }
        try {
            cl.setHeadimg(rs.getString(su + "headimg"));
        } catch (SQLException e) {
            cl.setHeadimg(null);
        }
        return cl;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, HomeworkComment cl) {
        par.put("chid", cl.getChid());
        par.put("hid", cl.getHid());
        par.put("uid", cl.getUid());
        par.put("content", cl.getContent());
        par.put("img", cl.getImg());
        par.put("cname", cl.getCname());
        par.put("cdate", cl.getCdate());
        par.put("headimg", cl.getHeadimg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            HomeworkComment cl) {
        args.put("chid", cl.getChid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final HomeworkComment cl) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(cl),
                        new String[] { "chid" });
                parsePS(ps, cl);
                return ps;
            }

        };
    }

    private static String parseSQL(HomeworkComment cl) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into homework_comment(");
        int count = 0;

        if (null != cl.getHid()) {
            sql.append("hid,");
            count++;
        }

        if (null != cl.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null != cl.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != cl.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != cl.getCname()) {
            sql.append("cname,");
            count++;
        }
        if (null != cl.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, HomeworkComment cl)
        throws SQLException {
        int count = 1;
        if (null != cl.getHid()) {
            ps.setLong(count++, cl.getHid());
        }

        if (null != cl.getUid()) {
            ps.setLong(count++, cl.getUid());
        }

        if (null != cl.getContent()) {
            ps.setString(count++, cl.getContent());
        }
        if (null != cl.getImg()) {
            ps.setString(count++, cl.getImg());
        }
        if (null != cl.getCname()) {
            ps.setString(count++, cl.getCname());
        }
        
        if (null != cl.getCdate()) {
            ps.setTimestamp(count++, cl.getCdate());
        }
        
    }

}
